package com.th.supcom.pds.dispensedrug.client.widget;

import net.carefx.framework.container.client.context.AppContext;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Component;
import com.th.supcom.pds.common.client.commom.FocusEvents;
import com.th.supcom.pds.common.client.model.PdsSupplyDetailModel;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;
import com.th.supcom.pds.common.constant.FocusConstants;
import com.th.supcom.pds.common.constant.KeysConstants;
import com.th.supcom.pds.common.utils.ClientUtils;

/**
 * 核对发药时间监听
 * 
 * @author 杨冲
 */
public class DispenseDrugListener
{
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";
    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";
    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
    private DispenseDrugAction m_dispenseDrugAction;
    private DispenseDrugButtons m_dispenseDrugButtons;
    private DispenseDrugFields m_dispenseDrugFields;
    private FocusEvents m_focusEvents;

    private Component m_keyNavComponent;

    private DispenseDrugRecipePanel m_recipePanel;

    /**
     * 添加清屏回车事件
     */
    private void addClearListener ()
    {
        m_dispenseDrugButtons.getClearBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                m_dispenseDrugAction.clear ();
            }

        });
    }

    /**
     * 添加发药事件，点击发药按钮时，进行发药操作，并刷新处方主记录
     */
    private void addDisepenseDrugListener ()
    {
        m_dispenseDrugButtons.getDispenseBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                m_dispenseDrugAction.dispenseDrug (m_dispenseDrugFields.getConsumeProofField (), m_dispenseDrugFields.getConsumeProofField ());
            }
        });
    }

    /**
     * 监听快捷键
     */
    private void addEnterKeyListener ()
    {
        new KeyNav <ComponentEvent> (m_keyNavComponent)
        {
            @Override
            public void onKeyPress (final ComponentEvent ce)
            {
                final int keyCode = ce.getKeyCode ();

                if (keyCode == KeysConstants.VK_ENTER)
                {
                    executEnterEvent ();
                }

                if (ce.isAltKey ())
                {
                    executAltEvent (keyCode);
                }

            }
        };
    }

    /**
     * 监听焦点事件
     */
    private void addFocusListeners ()
    {
        m_focusEvents = new FocusEvents ();
        m_focusEvents.add (FocusConstants.DISPENSE_DRUG_CLEAR_BTN, m_dispenseDrugButtons.getClearBtn ());
        m_focusEvents.add (FocusConstants.DISPENSE_DRUG_DISPENSE_BTN, m_dispenseDrugButtons.getDispenseBtn ());
        m_focusEvents.add (FocusConstants.DISPENSE_DRUG_PRESC_NO, m_dispenseDrugFields.getRecipeNoField ());
        m_focusEvents.add (FocusConstants.DISPENSE_DRUG_QUERY_BTN, m_dispenseDrugButtons.getQueryBtn ());
        m_focusEvents.add (FocusConstants.DISPENSE_DRUG_RCPT_NO, m_dispenseDrugFields.getConsumeProofField ());
        ClientUtils.getFocusChange (m_dispenseDrugFields.getRecipeNoField (), m_dispenseDrugButtons.getDispenseBtn (),
                                    m_dispenseDrugButtons.getQueryBtn ());
        ClientUtils.getFocusChange (m_dispenseDrugButtons.getQueryBtn (), m_dispenseDrugButtons.getClearBtn (),
                                    m_dispenseDrugButtons.getDispenseBtn ());
        ClientUtils.getFocusChange (m_dispenseDrugButtons.getDispenseBtn (), null, m_dispenseDrugButtons.getClearBtn ());
    }

    /**
     * 添加查询事件，但点击查询按钮时，根据输入的消费凭证号和处方号进行查询获得处方明细信息
     */
    private void addQueryByClickButtonListener ()
    {
        m_dispenseDrugButtons.getQueryBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                m_dispenseDrugAction.findAllRecipeDataByRcptNo (m_dispenseDrugButtons.getDispenseBtn (), m_focusEvents.getFocusComponent ());
            }
        });
    }

    /**
     * 添加查询处方明细事件，当点击处方主表时，触发此事件
     */
    private void addQueryRecipeDetailListener ()
    {
        m_recipePanel.getGridPanel ().getGrid ().addListener (Events.RowClick, new Listener <GridEvent <PdsSupplyModel>> ()
        {

            @Override
            public void handleEvent (final GridEvent <PdsSupplyModel> be)
            {
                final PdsSupplyModel model = be.getModel ();
                final PdsSupplyDetailModel detailModel = new PdsSupplyDetailModel ();
                detailModel.setPrescNo (model.getPrescNo ());
                detailModel.setPerformedBy (ClientUtils.getOrgModel().getOrgCode()); 
                m_dispenseDrugAction.findRecipeDetailData (detailModel);
            }

        });
    }

    /**
     * 执行Alt+?快捷键
     */
    private void executAltEvent (final int keyCode)
    {
        if (keyCode == KeysConstants.VK_Q)
        {
            m_dispenseDrugAction.findAllRecipeDataByRcptNo (m_dispenseDrugButtons.getDispenseBtn (), m_focusEvents.getFocusComponent ());
            return;
        }

        if (keyCode == KeysConstants.VK_O)
        {
            m_dispenseDrugAction.dispenseDrug (m_dispenseDrugFields.getConsumeProofField (), m_dispenseDrugFields.getConsumeProofField ());
            return;
        }

        if (keyCode == KeysConstants.VK_R)
        {
            m_dispenseDrugAction.clear ();
            return;
        }
    }

    /**
     * 执行回车事件
     */
    private void executEnterEvent ()
    {
        if (m_focusEvents.getFocusStatus (FocusConstants.DISPENSE_DRUG_PRESC_NO)
            || m_focusEvents.getFocusStatus (FocusConstants.DISPENSE_DRUG_RCPT_NO))
        {
            m_dispenseDrugAction.findAllRecipeDataByRcptNo (m_dispenseDrugButtons.getDispenseBtn (), m_focusEvents.getFocusComponent ());
            return;
        }
    }

    /**
     * 初始化所有的Listener
     */
    public void initListeners ()
    {
        addQueryByClickButtonListener ();
        addQueryRecipeDetailListener ();
        addDisepenseDrugListener ();
        addClearListener ();
        addEnterKeyListener ();
        addFocusListeners ();
    }

    public void setDispenseDrugAction (final DispenseDrugAction dispenseDrugAction)
    {

        m_dispenseDrugAction = dispenseDrugAction;
    }

    public void setDispenseDrugButtons (final DispenseDrugButtons dispenseDrugButtons)
    {
        m_dispenseDrugButtons = dispenseDrugButtons;
    }

    public void setDispenseDrugFields (final DispenseDrugFields dispenseDrugFields)
    {
        m_dispenseDrugFields = dispenseDrugFields;
    }

    public void setKeyNavComponent (final Component keyNavComponent)
    {
        m_keyNavComponent = keyNavComponent;
    }

    public void setRecipePanel (final DispenseDrugRecipePanel recipePanel)
    {

        m_recipePanel = recipePanel;
    }
}
